package org.example.util;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 工具类
 * 工具方法可能会出现异常，业务调用代码需要处理
 * try {
 * return
 * } catch (Exception e) {
 * LOGGER.error(e.getMessage());
 * return null;
 * }
 */
/*
    try {
        return
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        return null;
    }
*/
public class Utils {
    private static final DateTimeFormatter DATE_TIME_FORMATTER_CN = DateTimeFormatter.ofPattern("yyyy年M月d日 HH:mm:ss");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER_CN = DateTimeFormatter.ofPattern("yyyy年M月d日");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d");
    private static final SimpleDateFormat DATE_FORMAT_CN = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /*----------------------------------sql.Date-------------------------------------------*/

    public static Date str2Date2(String str) throws Exception {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        Date date = null;
        if (str.contains("年")) {
            LocalDate localDate = LocalDate.parse(str, DATE_FORMATTER_CN);
            date = Date.valueOf(localDate);
        } else if (str.contains("-")) {
            LocalDate localDate = LocalDate.parse(str, DATE_FORMATTER);
            date = Date.valueOf(localDate);
        }

        return date;
    }

    /*----------------------------------LocalDate-------------------------------------------*/

    public static LocalDate str2Date(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        LocalDate localDate = null;
        if (str.contains("-")) {
            localDate = LocalDate.parse(str.trim(), DATE_FORMATTER);
        } else if (str.contains("年")) {
            localDate = LocalDate.parse(str.trim(), DATE_FORMATTER_CN);
        }
        return localDate;
    }

    public static LocalDate timestamp2Date(Long timestamp) {
        if (null == timestamp || 0 == timestamp) {
            return null;
        }
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
        return localDateTime.toLocalDate();
    }

    public static String date2Str(LocalDate dateTime) {
        if (null == dateTime) {
            return null;
        }

        String format = dateTime.format(DATE_FORMATTER);
        return format;
    }

    /*----------------------------------LocalDateTime-------------------------------------------*/

    public static LocalDateTime str2Datetime(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        LocalDateTime dateTime = null;
        if (str.contains("-")) {
            dateTime = LocalDateTime.parse(str.trim(), DATE_TIME_FORMATTER);
        } else if (str.contains("年")) {
            dateTime = LocalDateTime.parse(str.trim(), DATE_TIME_FORMATTER_CN);
        }
        return dateTime;
    }

    public static LocalDateTime timestamp2Datetime(Long timestamp) {
        if (null == timestamp || 0 == timestamp) {
            return null;
        }
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8));
        return localDateTime;
    }

    public static String datetime2Str(LocalDateTime dateTime) {
        if (null == dateTime) {
            return null;
        }

        String format = dateTime.format(DATE_TIME_FORMATTER);
        return format;
    }

    /*----------------------------------Double---------------------------------------------*/

    public static Double str2Double(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }
        Double d = Double.valueOf(str.trim());
        return d;
    }

    public static String double2Str(Double d) {
        if (null == d) {
            return null;
        }
        String s = String.valueOf(d.doubleValue());
        return s;
    }

    /*----------------------------------Decimal---------------------------------------------*/

    public static BigDecimal str2Decimal(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        BigDecimal bigDecimal = new BigDecimal(str.trim());
        return bigDecimal;
    }

    public static BigDecimal double2Decimal(Double d) {
        if (null == d) {
            return null;
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(d);
        return bigDecimal;
    }

    /*----------------------------------Integer---------------------------------------------*/

    public static Integer str2Integer(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }
        Integer d = Integer.valueOf(str.trim());
        return d;
    }

    /*----------------------------------String---------------------------------------------*/

    public static String str2String(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }
        return str;
    }
    public static String obj2String(Object obj) {
        if (null == obj) {
            return null;
        }
        return obj.toString();
    }

    /*----------------------------------Long---------------------------------------------*/
    public static Long str2Long(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }
        return Long.valueOf(str.trim());
    }

    public static Date strToDate(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        Date date = null;
        if (str.contains("-")) {
            date = Date.valueOf(LocalDate.parse(str.trim(), DATE_FORMATTER));
        } else if (str.contains("年")) {
            date = Date.valueOf(LocalDate.parse(str.trim(), DATE_FORMATTER_CN));
        }
        return date;
    }

    public static String integer2Str(Integer in) {
        if (null == in) {
            return null;
        }
        String s = String.valueOf(in.intValue());
        return s;
    }

    /*----------------------------------regex---------------------------------------------*/

    public static Double getDoubleFromStr(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        Pattern pattern = Pattern.compile("[^0-9\\.]");
        Matcher matcher = pattern.matcher(str.trim());
        String s = matcher.replaceAll("").trim();
        Double aDouble = Double.valueOf(s);
        return aDouble;
    }
    public static BigDecimal getDecimalFromStr(String str) {
        if (null == str || "".equals(str.trim())) {
            return null;
        }

        Pattern pattern = Pattern.compile("[^0-9\\.]");
        Matcher matcher = pattern.matcher(str.trim());
        String s = matcher.replaceAll("").trim();
        Double aDouble = Double.valueOf(s);
        BigDecimal decimal = BigDecimal.valueOf(aDouble);
        return decimal;
    }
}
